package site.yunnong.atvris.portal.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import site.yunnong.atvris.common.api.ResultVO;
import site.yunnong.atvris.portal.service.UserSsoService;
import site.yunnong.atvris.service.UserService;
import site.yunnong.atvris.common.dto.UserLoginFormParam;
import site.yunnong.atvris.common.dto.UserRegisterFormParam;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author JinghaoZhang
 * @date 2021-03-20 22:19
 */
@Slf4j
@Api(tags = {"单点登录"})
@RestController
@RequestMapping("/sso")
//@Validated
public class AccountSSOController {

    @Resource
    private UserSsoService userSsoService;
    @Resource
    private UserService userService;

    @Value("${jwt.tokenHead}")
    private String tokenHead;
    
    @Value("")
    private String a;

    @ApiOperation("获取验证码")
    @GetMapping("/authCode")
    public ResultVO getAuthCode(@RequestParam String username) {
        String authcode = userSsoService.generateAuthCode(username);
        return new ResultVO().success(authcode);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public ResultVO register(@RequestBody UserRegisterFormParam param) {
        String token = userSsoService.register(param);
        Map<String, String> tokenMap = new HashMap<>(2);
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return new ResultVO().success(tokenMap);
    }

    /**
     * todo 暂时先用username + password 登录 ，以后再加验证码、短信登录、扫码登录等
      */
    @ApiOperation("密码登录并返回token")
    @PostMapping("/login")
    public ResultVO login(@RequestBody UserLoginFormParam userLoginFormParam) {
        String username = userLoginFormParam.getUsername();
        String password = userLoginFormParam.getPassword();
        String authCode = userLoginFormParam.getAuthCode();
        String token = userSsoService.login(username, password, authCode);
        Map<String, String> tokenMap = new HashMap<>(2);
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return new ResultVO().success(tokenMap);
    }
    
    @ApiOperation("用户名是否重复")
    @GetMapping("/is-username-exist")
    public ResultVO isUsernameExist(@RequestParam String username) {
        return new ResultVO().success(userService.countUserByUsername(username));
    }

    @ApiOperation("昵称是否重复")
    @GetMapping("/is-nickname-exist")
    public ResultVO isNicknameExist(@RequestParam String nickname) {
        return new ResultVO().success(userService.countUserByNickname(nickname));
    }

    @ApiOperation(value = "刷新token")
    @GetMapping(value = "/refresh-token")
    public ResultVO refreshToken(String tokenWithHeader) {
        String refreshToken = userSsoService.refreshToken(tokenWithHeader);
        Map<String, String> tokenMap = new HashMap<>(2);
        tokenMap.put("token", refreshToken);
        tokenMap.put("tokenHead", tokenHead);
        return new ResultVO().success(tokenMap);
    }

    @ApiOperation("检查登录状态")
    @GetMapping("/is-login")
    public ResultVO isLogin() {
        return new ResultVO().success(userSsoService.isLogin());
    }
}
